#!/usr/bin/python 

from scapy.all import *
import time
import logging

logger = logging.getLogger("scantopo.icmpRtt")

def icmp(dip, dmac, count):
    logger.debug("icmprtt debug")
    #builNet.py ---> autoStaticArp=True
    #arp = Ether()/ARP(op="who-has",hwdst="00:00:00:00:00:00", pdst="10.0.0.1")
    #arp = Ether()/ARP(op="is-at",hwdst=dmac, pdst=dip)
    #print "send"
    #sendp(arp)
    #sendp(arp)
    #sendp(arp)
    #time.sleep(1)

    ether = Ether(dst = dmac,type=0x0800)
    ip = IP(dst = dip,proto =1)
    icmp = ICMP(type = 8)
    #data = Raw('i'*2048)
    #print len(data)
    
    pkt = ether/ip/icmp#/data
    #print len(pkt)
    total = 0.0
    a = 0
    for i in range(count):
        #ans,unans = srp(fragment(pkt),verbose=0,timeout=10)
        ans,unans = srp(pkt,verbose=0,timeout=10)
        if(len(ans) != 0):
            rx = ans[0][1]
            tx = ans[0][0]
            delta = rx.time - tx.sent_time
            #print("icmp time delay: ",delta*1000)
            #revise
            if i > 4:
                if delta*1000 < 200:
                    total += delta
                else:
                    a = a + 1
        else:
            print("fail")
    #print "actual count times",count-a
    return total/(count-a)*1000

if __name__ == "__main__":
    a = raw_input("dst host number:")
    print "00:00:00:00:00:0" + hex(int(a)).split('x')[1]
    average = icmp("10.0.0." + a, "00:00:00:00:00:0" + hex(int(a)).split('x')[1], 5)
    print average
